using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Timers;

using OWED.Network.Messages;
using OWED.Network.Messages.General;
using OWED.Network.Messages.Reply;
using OWED.Network;

using System.Xml;

using ClientGUI.Widgets;

namespace ClientGUI.Main.PopUpWindows
{
    public partial class ImportDocument : NetworkDialog
    {
        MainForm my_main;
        List <String> my_doc_list;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="main">Parent form</param>
        public ImportDocument(MainForm main): base("Import Document:", main)
        {
            my_main = main;
            InitializeComponent();

            my_doc_list = new List<string>();

            GetDocumentList gl = new GetDocumentList(my_main.ProgramData.User);
            try
            {
                XmlDocument r = my_main.ProgramData.Connection.Send(gl.ToXml());

                IProtocolItem reply = MessageGet.GetFromXml(r);
                if ((reply as PassFail) != null)
                {
                    PassFail f = reply as PassFail;
                    StatusMessage(f.Reason.Value);
                }
                else
                {
                    DocumentListReply list = reply as DocumentListReply;
                    for (int i = 0; i < list.DocumentNames.Count; ++i)
                    {
                        my_doc_list.Add(list.DocumentNames[i].name);
                    }
                }
            }
            catch (ConnectionException exc)
            {
                my_main.Error(exc.Message);
                my_main.Connected = false;
                Dispose();
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            OWED.Network.Messages.ImportDocument import = 
                new OWED.Network.Messages.ImportDocument(
                    my_main.ProgramData.User,
                    new DocName(textBox1.Text),
                    new ADocument(my_main.GetDocument(), new DocName(textBox1.Text)));



            OWEDReply call = delegate(XmlDocument re)
            {
                IProtocolItem reply = MessageGet.GetFromXml(re);

                PassFail rpass = reply as PassFail;
                if (rpass != null)
                {
                    Invoke(new StringMethodCall(StatusMessage), rpass.Reason.Value);
                    if (rpass.pass)
                    {
                        Invoke(new MethodInvoker(Dispose));
                    }
                }
                else
                {
                    StatusMessage("Failed to create for unknown reason");
                }
            };
            my_main.ProgramData.Connection.Send(import.ToXml(), call);
            StatusMessage("Uploading document...");
        }

        System.Timers.Timer my_check_wait = null;

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (my_check_wait != null)
                my_check_wait.Dispose();
            my_check_wait = new System.Timers.Timer(500);

            my_check_wait.Elapsed += new ElapsedEventHandler(my_check_wait_Elapsed);

            my_check_wait.Enabled = true;
            my_check_wait.Start();

            button1.Enabled = false;
        }

        void enable_check()
        {
            if (InvokeRequired)
                Invoke(new MethodInvoker(enable_check));
            button1.Enabled = !my_doc_list.Contains(textBox1.Text);
        }

        void my_check_wait_Elapsed(object sender, ElapsedEventArgs e)
        {
            enable_check();   
        }

        private void cancel_btn_Click(object sender, EventArgs e)
        {
            Dispose();
        }
    }
}
